package com.hb.core.pojo.parent;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonFilter;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;

@Setter
@Getter
@ToString
@JsonFilter("entityFilter")
public class MPPageEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableField(exist = false)
    public transient final static int DEFAULT_CURRENT = 1;

    @TableField(exist = false)
    public transient final static int DEFAULT_LIMIT = 10;

    /**
     * 当前页数
     */
    @TableField(exist = false)
    @ApiModelProperty(value = "当前页数")
    protected Integer currentPage;

    /**
     * 每页条数
     */
    @TableField(exist = false)
    @ApiModelProperty(value = "每页条数")
    protected Integer pageSize;

    /**
     * 数据总数
     */
    @TableField(exist = false)
    @ApiModelProperty(value = "数据总数")
    protected Integer total;

    /**
     * 获取mybatisplus的分页对象
     *
     * @param <T>
     * @return
     */
    public <T> Page<T> pages() {
        Integer currentPage = this.getCurrentPage();
        if (currentPage == null) {
            currentPage = DEFAULT_CURRENT;
        }
        Integer pageSize = this.getPageSize();
        if (pageSize == null) {
            pageSize = DEFAULT_LIMIT;
        }
        return new Page<>(currentPage, pageSize);
    }

    /**
     * hasPage方法如果返回true, 表示page参数由前端传递
     *
     * @return
     */
    public boolean hasPage() {
        return currentPage != null;
    }

}
